﻿using System;
using System.Windows.Forms;
using System.Diagnostics;
using DevExpress.LookAndFeel;
using DevExpress.XtraEditors;
using S3.Common;

namespace S3
{
    static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main()
        {
            try
            {
                //Check allow only 1 instance
                //LogHelper log = new LogHelper();
                //log.WriteLog("Program");
                if (!IsExistingInstance())
                {
                    //log.WriteLog("NOT IsExistingInstance");
                    Application.EnableVisualStyles();

                    Application.SetCompatibleTextRenderingDefault(false);
                    DevExpress.Skins.SkinManager.EnableFormSkins();                                     
                    UserLookAndFeel.Default.SetSkinStyle("DevExpress Style");
#if S3_RELEASE
                    if (IsRegisterd())
                    {
                        //log.WriteLog("Is Registerd");                        
                        Application.Run(new LoginForm());                        
                    }
                    else
                    {
                        //log.WriteLog("Is NOT Registerd");
                        Application.Run(new LisenceForm());
                    }
#else
                    Application.Run(new LoginForm());                        
#endif

                }
            }
            catch (Exception ex)
            {
            }
            //MainForm main = new MainForm();
            //if (!main.IsDisposed)
            //{
            //    main.ShowDialog();
            //}
            //var path = ConfigurationSettings.AppSettings.AllKeys.FirstOrDefault(x => x.Equals("PSMEntities"));

            //path = string.Format(path, Path.Combine(Application.StartupPath, Constants.DBPath));


            //var frmLisenceForm = new LisenceForm();
            //if (!frmLisenceForm.IsDisposed && frmLisenceForm != null)
            //{
            //    frmLisenceForm.ShowDialog();                
            //}



            //}
            //else
            //{                
            //    Application.ExitThread();
            //    Application.Exit();
            //}
        }

        private static bool IsRegisterd()
        {
            ModifyRegistry registry = new ModifyRegistry();
            try
            {
                var lisenceKey = registry.Read(Constants.RegistryLisenceKey);

                //Not exist key
                if (string.IsNullOrEmpty(lisenceKey))
                {
                    return false;
                }
                else
                {
                    //Check is trial or full
                    var lisenceExpire = registry.Read(Constants.RegistryLisenceExpire);
                    if (string.IsNullOrEmpty(lisenceExpire))//Expired
                    {
                        return false;
                    }
                    else
                    {
                        DateTime expireDate = new DateTime();
                        if (DateTime.TryParse(lisenceExpire, out expireDate))
                        {
                            if (expireDate <= DateTime.Now)
                            {
                                XtraMessageBox.Show("Phần mềm đã hết hạn sử dụng !");

                                return false;
                            }
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("Loi trong qua trinh khoi dong he thong. " + ex.Message);
            }

            return true;
        }

        static bool IsExistingInstance()
        {
            foreach (Process p in Process.GetProcesses())
            {
                if (!string.IsNullOrEmpty(p.ProcessName) && p.ProcessName.ToLower().Trim().Equals("s3.exe"))
                {
                    return true;
                }
            }

            return false;

        }
    }
}